package com.deft.design.memo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 管理类：用于操作记录备忘信息
 *
 * @author Hide丶love
 * @date 2022/05/18
 **/
public class Admin {

    private int cursorIdx = 0;
    private List<ConfigMemento> mementos = new ArrayList<>();
    private Map<String, ConfigMemento> mementoMap = new HashMap<>();

    public void append(ConfigMemento memento) {
        mementos.add(memento);
        mementoMap.put(memento.getConfigFile().getVersionNo(), memento);
        cursorIdx++;
    }

    public ConfigMemento undo() {
        if (--cursorIdx <= 0) {
            return mementos.get(0);
        }
        return mementos.get(cursorIdx);
    }

    public ConfigMemento redo() {
        if (++cursorIdx > mementos.size()) {
            return mementos.get(mementos.size() - 1);
        }
        return mementos.get(cursorIdx);
    }

    public ConfigMemento get(String versionNo) {
        return mementoMap.get(versionNo);
    }
}
